import {ADD_COUNTER,ADD_TO_CART} from "./mutations-types";


export default {
  addCart(context, payload) {
    //payload:新添加的商品
    return new Promise((resolve, reject) => {
      //查找之前的数组中是否有该商品
      let oldProduct = context.state.cartList.find(item => item.iid === payload.iid)

      //判断oldProduct的值
      if (oldProduct) {
        //同一种商品的数量加一
        context.commit(ADD_COUNTER, oldProduct)
        resolve('当前商品的数量已+1')
      } else {
        //添加新的商品
        payload.count = 1
        context.commit(ADD_TO_CART, payload)
        resolve('商品已加入购物车~')
      }
    })
  }
}
